home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 October: Mac OS SDK / Dev.CD Oct 00 SDK1.toast / Development Kits / Mac OS / Multiprocessing 2.1v2 SDK / Sample Code / HappyTrails ƒ / µApp.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-25  |  9.0 KB  |  496 lines  |  [TEXT/CWIE]

  1. /**\
  2. |**|    µApp.c
  3. \**/
  4.  
  5. /**\
  6. |**|    compiler directives
  7. \**/
  8.  
  9. #define SystemSevenOrLater        1
  10.  
  11. /**\
  12. |**|    Includes
  13. \**/
  14.  
  15. #include <Devices.h>
  16. #include <Fonts.h>
  17. #include <CodeFragments.h>
  18. #include <Dialogs.h>
  19. #include <DiskInit.h>
  20. #include <LowMem.h>
  21. #include <fp.h>
  22. #include <PLStringFuncs.h>
  23. #include <Sound.h>
  24. #include <Traps.h>
  25. #include <Threads.h>
  26.  
  27. #include <stdio.h>
  28. #include <string.h>
  29.  
  30. #include "HappyTrails.h"
  31.  
  32. /**\
  33. |**|    typedefs, structs, enums, etc.
  34. \**/
  35.  
  36. enum
  37. {
  38.     mAppleMenu = 128,
  39.         iAboutBox = 1,
  40.     mFileMenu = 129,
  41.         iQuit = 1,
  42.     mTasksMenu = 130,
  43.     mWeightMenu = 131
  44. };
  45.  
  46. /**\
  47. |**|    private (local) function prototypes
  48. \**/
  49.  
  50. static OSErr Init_Mac(void);
  51. static void Handle_Command(long ms);
  52. static void Handle_Event(const EventRecord *pEventPtr);
  53. static void Handle_NullEvent(const EventRecord *pEventPtr);
  54. static void Handle_MouseEvent(const EventRecord *pEventPtr);
  55. static void Handle_KeyEvent(char key,SInt16 modifiers);
  56. static void Handle_UpdateEvent(WindowPtr updateWindowP);
  57. //static void Handle_ActivateEvent(WindowPtr updateWindowP);
  58. static void Handle_DiskEvent(long message);
  59. static void Handle_OSEvent(long message);
  60.  
  61. static Boolean SetUp_MenuBar(void);
  62. static void Adjust_MenuItems(void);
  63. static void Adjust_Cursor(const Boolean pContextual);
  64.  
  65. /**\
  66. |**|    globals & constants
  67. \**/
  68.  
  69. WindowPtr    gWindowPtr = nil;
  70. Rect        gWindowRect;
  71. Boolean        gInBackGround = false;
  72. Rect         gMenuRect = {0,0,0,0};
  73.  
  74. static Boolean gQuitFlag = false;
  75.  
  76. /**\
  77. |**|    main
  78. \**/
  79.  
  80. void main(void)
  81. {
  82.     if (!Init_Mac() && SetUp_MenuBar() && !HT_Init(9))
  83.     {
  84.         do
  85.         {
  86.             EventRecord event;
  87.  
  88.             WaitNextEvent(everyEvent,&event,-1,nil);
  89.             Adjust_Cursor((event.modifiers & controlKey) == controlKey);
  90.             Handle_Event(&event);
  91.         }
  92.         while (!gQuitFlag);
  93.  
  94.         HT_Term();
  95.     }
  96. }
  97.  
  98. /**\
  99. |**|    Initialize toolboxes
  100. \**/
  101.  
  102. static OSErr Init_Mac(void)
  103. {
  104.     OSErr        error = noErr;
  105. #if TARGET_API_MAC_CARBON
  106.     BitMap        screenBits;
  107. #else
  108.     SysEnvRec    theWorld;
  109.         
  110.     //
  111.     //    Test the computer to be sure we can do color.  
  112.     //    If not we would crash, which would be bad.  
  113.     //    If we can’t run, just beep and exit.
  114.     //
  115.     error = SysEnvirons(1, &theWorld);
  116.     if (theWorld.hasColorQD == false)
  117.     {
  118.         SysBeep(50);
  119.         ExitToShell();                    // If no color QD, we must leave.
  120.     }
  121.     
  122.     MaxApplZone();
  123.     InitGraf(&(qd.thePort));
  124.     InitFonts();
  125.     InitWindows();
  126.     InitMenus();
  127.     TEInit();
  128.     InitDialogs(nil);
  129. #endif TARGET_API_MAC_CARBON
  130.  
  131.     InitContextualMenus();
  132.  
  133.     //
  134.     //    Make a new window for drawing in, and it must be a color window.  
  135.     //    The window is full screen size, made smaller to make it more visible.
  136.     //
  137.  
  138. #if TARGET_API_MAC_CARBON
  139.     gWindowRect = GetQDGlobalsScreenBits(&screenBits)->bounds;
  140. #else
  141.     gWindowRect = qd.screenBits.bounds;
  142. #endif TARGET_API_MAC_CARBON
  143. //    InsetRect(&gWindowRect, 20, 20);
  144.     gWindowRect.top += GetMBarHeight() + 1;
  145.  
  146.     gWindowPtr = NewCWindow(nil, &gWindowRect, "\pHappy Trails!", true, documentProc, 
  147.                         (WindowPtr) -1, false, 0);
  148.  
  149.     OffsetRect(&gWindowRect, -gWindowRect.left, -gWindowRect.top);
  150.  
  151. #if TARGET_API_MAC_CARBON
  152.     SetPort(GetWindowPort(gWindowPtr));        // set window to current graf port
  153. #else
  154.     SetPort(gWindowPtr);                    // set window to current graf port
  155. #endif TARGET_API_MAC_CARBON
  156.     TextSize(kTextSize);                    // smaller font for drawing.
  157.  
  158.     return error;
  159. }
  160.  
  161. /**\
  162. |**|    Menu hooks
  163. \**/
  164.  
  165. static pascal short MyMBarHook(Rect *menuRect)
  166. {
  167. //    RgnHandle tempRgn = NewRgn();
  168.  
  169.     gMenuRect = *menuRect;
  170.  
  171. //    RectRgn(tempRgn,menuRect);
  172. //    CalcVisBehind(LMGetWindowList(),tempRgn);
  173. //    InvertRgn(tempRgn);
  174.  
  175.     //YieldToAnyThread();
  176. //    DisposeRgn(tempRgn);
  177.     return 0;
  178. }
  179.  
  180. static pascal void MyMenuHook(void)
  181. {    
  182. //    RgnHandle tempRgn = NewRgn();
  183. //    RectRgn(tempRgn,&gMenuRect);
  184. //    CalcVisBehind(LMGetWindowList(),tempRgn);
  185. //    InvertRgn(tempRgn);
  186.  
  187.     //YieldToAnyThread();
  188. //    DisposeRgn(tempRgn);
  189. }
  190.  
  191. /**\
  192. |**|    Setup menu bar
  193. \**/
  194.  
  195. static Boolean SetUp_MenuBar(void)
  196. {
  197.     Handle mBar = GetNewMBar(128);    // handle to menu bar resource
  198.  
  199.     if (!ResError() && mBar)
  200.     {
  201.         SetMenuBar(mBar);
  202.         AppendResMenu(GetMenuHandle(mAppleMenu),'DRVR');
  203.         DrawMenuBar();
  204.         ReleaseResource(mBar);
  205.         Adjust_MenuItems();
  206.  
  207. //        LMSetMBarHook(NewMBarHookProc(MyMBarHook));
  208. //        LMSetMenuHook(NewMenuHookProc(MyMenuHook));
  209.  
  210.         return true;
  211.     }
  212.     return false;
  213. }
  214.  
  215. /**\
  216. |**|    Adjust menu items
  217. \**/
  218.  
  219. static void Adjust_MenuItems(void)
  220. {
  221.     MenuHandle tMenuHdl = GetMenuHandle(mTasksMenu);
  222.     if (tMenuHdl)
  223.     {
  224.         UInt16 index,count = CountMenuItems(tMenuHdl);
  225.         for (index = 1;index <= count;index++)
  226.             CheckMenuItem(tMenuHdl, index, index == gNumTasks);
  227.     }
  228. }
  229.  
  230. /**\
  231. |**|    Adjust cursor
  232. \**/
  233.  
  234. static void Adjust_Cursor(const Boolean pContextual)
  235. {
  236.     if (pContextual)
  237.     {
  238.         static CursHandle tCursHandle = nil;
  239.         if (!tCursHandle)
  240.             tCursHandle = GetCursor(128);    // kThemeContextualMenuArrowCursor?
  241.         if (tCursHandle)
  242.             SetCursor(*tCursHandle);
  243.     }
  244.     else
  245. #if    TARGET_API_MAC_CARBON
  246.     {
  247.         Cursor            arrowCursor;
  248.         SetCursor(GetQDGlobalsArrow(&arrowCursor));
  249.     }
  250. #else
  251.         SetCursor(&qd.arrow);
  252. #endif TARGET_API_MAC_CARBON
  253. }
  254.  
  255. /**\
  256. |**|    Do menu command
  257. \**/
  258.  
  259. static void Handle_Command(long ms)
  260. {
  261.     short    menuID = ms >> 16,
  262.             menuItem = ms & 0xFFFF;
  263.  
  264.     switch (menuID)
  265.     {
  266.         case    mAppleMenu:
  267.             switch (menuItem)
  268.             {
  269.                 case iAboutBox:        // Bring up alert for About.
  270.                     SysBeep(15);
  271.                     break;
  272.                 default:            // All non-About items in this menu are DAs.
  273. #if !TARGET_API_MAC_CARBON
  274.                     {
  275.                         Str255    daName;
  276.                         GetMenuItemText(GetMenuHandle(menuID),menuItem,daName);
  277.                         OpenDeskAcc(daName);
  278.                     }
  279. #endif TARGET_API_MAC_CARBON
  280.                     break;
  281.             }
  282.             break;
  283.         case    mFileMenu:
  284.             switch (menuItem)
  285.             {
  286.                 case iQuit:
  287.                     gQuitFlag = true;
  288.                     break;
  289.                 default:
  290.                     SysBeep(15);
  291.                     break;
  292.             }
  293.             break;
  294.         case    mTasksMenu:
  295.             HT_Term();
  296.             HT_Init(menuItem);
  297.             break;
  298.         case    mWeightMenu:        // Weight Menu
  299.             break;
  300.     }
  301. }
  302.  
  303. /**\
  304. |**|    Do event
  305. \**/
  306.  
  307. static void Handle_Event(const EventRecord *pEventPtr)
  308. {
  309.     switch (pEventPtr->what)
  310.     {
  311.         case nullEvent:
  312.             Handle_NullEvent(pEventPtr);
  313.             break;
  314.         case mouseDown:
  315.             Handle_MouseEvent(pEventPtr);
  316.             break;
  317.         case keyDown:
  318.         case autoKey:
  319. dokey:
  320.             Handle_KeyEvent((char)(pEventPtr->message & charCodeMask),pEventPtr->modifiers);
  321.             break;
  322.         case updateEvt:
  323.             Handle_UpdateEvent((WindowPtr)pEventPtr->message);
  324.             break;
  325.         case diskEvt:
  326.             Handle_DiskEvent(pEventPtr->message);
  327.             break;
  328.         case osEvt:
  329.             Handle_OSEvent(pEventPtr->message);
  330.             break;
  331.         case kHighLevelEvent:
  332.             AEProcessAppleEvent(pEventPtr);
  333.             break;
  334.         default:
  335.             break;
  336.     }
  337. }
  338.  
  339. /**\
  340. |**|    Do null event
  341. \**/
  342.  
  343. static void Handle_NullEvent(const EventRecord *pEventPtr)
  344. {
  345.     (pEventPtr);    // #pragma unused (pEventPtr)
  346.     if (gWindowPtr)
  347.     {
  348.         Rect    tRect = {40,20,52,128};
  349.         const RGBColor blackRGBColor = {0,0,0}, yellowRGBColor = {255,255,0};
  350.         GrafPtr savePort;
  351.  
  352.         GetPort(&savePort);
  353. #if TARGET_API_MAC_CARBON
  354.         SetPort(GetWindowPort(gWindowPtr));
  355. #else
  356.         SetPort(gWindowPtr);
  357. #endif TARGET_API_MAC_CARBON
  358.  
  359.         HT_DoNull();
  360.  
  361.         SetPort(savePort);
  362.     }
  363. }
  364.  
  365. /**\
  366. |**|    Do mousedown event
  367. \**/
  368.  
  369. static void Handle_MouseEvent(const EventRecord *pEventPtr)
  370. {
  371.     Boolean tInBackGround = gInBackGround;
  372.     WindowPtr            window;
  373.     short                part;
  374.  
  375.     gInBackGround = true;    // suspend drawing while menus are displayed
  376.     HT_DoNull();
  377.  
  378.     part = FindWindow(pEventPtr->where,&window);
  379.  
  380.     Adjust_Cursor(part == inContent);
  381.  
  382.     switch(part)
  383.     {
  384.         case inContent:
  385.             {
  386.                 if (window != FrontWindow())
  387.                     SelectWindow(window);
  388.                 else
  389.                     HT_DoClick(pEventPtr);
  390.             }
  391.             break;
  392.  
  393.         case inMenuBar:        // Process mouse menu command (if any).
  394.             {
  395.                 long ms;
  396.  
  397.                 Adjust_MenuItems();
  398.                 ms = MenuSelect(pEventPtr->where);
  399.                 if (ms)
  400.                     Handle_Command(ms);
  401.                 HiliteMenu(0);        // Unhighlight what MenuSelect hilited.
  402.             }
  403.             break;
  404. #if !TARGET_API_MAC_CARBON
  405.         case inSysWindow:    // Let the system handle the mouseDown.
  406.             SystemClick(pEventPtr,window);
  407.             break;
  408. #endif TARGET_API_MAC_CARBON
  409.         default:
  410.             break;
  411.     }
  412.     gInBackGround = tInBackGround;
  413.  
  414.     SetRect(&gMenuRect, 0, 0, 0, 0);
  415. }
  416.  
  417. /**\
  418. |**|    Handle key events
  419. \**/
  420.  
  421. static void Handle_KeyEvent(char key,SInt16 modifiers)
  422. {
  423.     if ((modifiers & cmdKey) != 0)
  424.     {
  425.         Adjust_MenuItems();
  426.         Handle_Command(MenuKey(key));
  427.         HiliteMenu(0);        // Unhighlight what MenuSelect hilited.
  428.     }
  429. }
  430.  
  431. /**\
  432. |**|    Handle update events
  433. \**/
  434.  
  435. static void Handle_UpdateEvent(WindowPtr updateWindowP)
  436. {
  437.     GrafPtr savePort;
  438.     RgnHandle        visRgn;
  439.  
  440.     GetPort(&savePort);
  441.     visRgn=NewRgn();
  442.  
  443. #if TARGET_API_MAC_CARBON
  444.     SetPort(GetWindowPort(updateWindowP));
  445.     
  446.     GetPortVisibleRegion(GetWindowPort(FrontWindow()), visRgn);
  447. #else
  448.     SetPort(updateWindowP);
  449.     visRgn = NewRgn();
  450.     CopyRgn(updateWindowP->visRgn,visRgn);
  451. #endif TARGET_API_MAC_CARBON
  452.  
  453.     BeginUpdate(updateWindowP);
  454.     UpdateControls(updateWindowP,visRgn);
  455.     DrawGrowIcon(updateWindowP);
  456.     EndUpdate(updateWindowP);
  457.  
  458.     SetPort(savePort);
  459.  
  460.     DisposeRgn(visRgn);
  461. }
  462.  
  463. /**\
  464. |**|    Handle disk events
  465. \**/
  466.  
  467. static void Handle_DiskEvent(long message)
  468. {
  469. #if TARGET_API_MAC_CARBON
  470.     (message);
  471. #else
  472.     Point dialogLocation = {100,100};
  473.  
  474.     if ((message & 0xFFFF0000) != noErr)
  475.     {
  476.         DIBadMount(dialogLocation,message);
  477.     }
  478. #endif TARGET_API_MAC_CARBON
  479. }
  480.  
  481. /**\
  482. |**|    Handle OS events
  483. \**/
  484.  
  485. static void Handle_OSEvent(long message)
  486. {
  487.     if ((message >> 24) == suspendResumeMessage)
  488.     {
  489.         if ((message & resumeFlag) != 0)
  490.             gInBackGround = false;
  491.         else
  492.             gInBackGround = true;
  493.     }
  494. }
  495.  
  496.